package main

import (
	"fmt"
	"io"
	"os"
)

func main() {

	Copy1("D:\\Environment\\GoWorks\\src\\Go_Study_Project\\文件\\1.jpg", "D:\\Environment\\GoWorks\\src\\Go_Study_Project\\文件\\1-COPY.jpg", 10)

}
func Copy1(source, dest string, bssize int) (int, bool, error) {

	mubiaofile, mubiaoerr := os.OpenFile(dest, os.O_RDWR, os.ModePerm)
	backfile, backerror := os.OpenFile(source, os.O_RDWR, os.ModePerm)

	defer mubiaofile.Close()
	defer backfile.Close()
	if mubiaoerr != nil {
		file, _ := os.OpenFile(dest, os.O_CREATE, os.ModePerm)
		mubiaofile = file
	}
	if backerror != nil { //当需要复制的文件找不到时抛出错误同时传递false
		return 0, false, backerror

	}
	bs := make([]byte, bssize)
	sum := 0
	for {
		n, efoerr := backfile.Read(bs)
		fmt.Println("本次读取了", n, "个字节")
		fmt.Println("内容为", string(bs))
		if n == 0 || efoerr == io.EOF {
			fmt.Println("读取完毕")
			fmt.Println("总共读取了", sum, "字节")
			return sum, true, nil
		}
		n, _ = mubiaofile.Write(bs[:n])
		fmt.Println("写入了", n, "个字节")

		sum += n

	}

}
